// 2. 游戏控制模块 - 管理游戏状态 (无改动)
module game_controller(
    input clk,
    input rst_n,
    input start_btn,    // 开始按钮
    input reset_btn,    // 重置按钮
    input sequence_complete, // 序列完成信号
    output reg [1:0] game_state,    // 00:待机 01:游戏中 10:结束
    output reg game_start,
    output reg game_reset
);

    parameter IDLE = 2'b00;
    parameter PLAYING = 2'b01;
    parameter GAME_OVER = 2'b10;

    reg start_btn_d1, start_btn_d2;
    reg reset_btn_d1, reset_btn_d2;
    wire start_pressed, reset_pressed;

    // 按键边沿检测
    always @(posedge clk or negedge rst_n) 
    begin
        if (!rst_n) begin
            start_btn_d1 <= 1;
            start_btn_d2 <= 1;
            reset_btn_d1 <= 1;
            reset_btn_d2 <= 1;
        end 
        else 
        begin
            start_btn_d1 <= start_btn;
            start_btn_d2 <= start_btn_d1;
            reset_btn_d1 <= reset_btn;
            reset_btn_d2 <= reset_btn_d1;
        end
    end

    assign start_pressed = start_btn_d2 & (~start_btn_d1);
    assign reset_pressed = reset_btn_d2 & (~reset_btn_d1);

    always @(posedge clk or negedge rst_n) 
    begin
        if (!rst_n) begin
            game_state <= IDLE;
            game_start <= 0;
            game_reset <= 0;
        end else begin
            game_start <= 0;
            game_reset <= 0;

            case (game_state)
                IDLE: begin
                    if (start_pressed) begin
                        game_state <= PLAYING;
                        game_start <= 1;
                    end
                    if (reset_pressed) begin
                        game_reset <= 1;
                    end
                end

                PLAYING: begin
                    if (reset_pressed) 
                    begin
                        game_state <= IDLE;
                        game_reset <= 1;
                    end 
                    else if (sequence_complete) 
                    begin
                        game_state <= GAME_OVER;
                    end
                end

                GAME_OVER: 
                begin
                    if (start_pressed || reset_pressed) begin
                        game_state <= IDLE;
                        game_reset <= 1;
                    end
                end
            endcase
        end
    end
endmodule
